var app = getApp()
Page({
  data: {
    list:[],
    topCurrentId:'all',
    leftCurrentId:'all',
    topBtn:[],
    page:2,
    leftBtn:[],
    parent_id:0,
    goodsType:0,
    parentType:'',
    searchKey:'',
    account:'',
    agentId:''
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        account:res.data.id
      })
      this.getFirstTypeList(res.data.agentId)
      this.getGoodsTypeList(res.data.agentId,0)
    })
  },
  onReachBottom(){
    let page = this.data.page
    this.getGoodsList(page,this.data.searchKey)
    page++
    this.setData({
      page
    })
  },
  searchInput(e){
    let searchKey = e.detail.value
    this.setData({
      searchKey
    })
  },
  searchConfirm(){
    this.getGoodsList(1,this.data.searchKey)
  },
  getGoodsList(pageNum,searchKey){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      list:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum,
      searchKey,
    }
    console.log(data)
    let list = []
    app.request('wx_app/stock_all/sku/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (res.data.pageContent!='') {
          res.data.pageContent.forEach(item=>{
            list.push({
              name:item.product_name,
              price:item.price,
              url:item.main_img_url,
              id:item.id,
              type:item.product_type,
              isAdded:item.isAdded,
              companyName:item.productTag,
              color:item.tagColor
            })
            that.setData({
              list
            })
            wx.hideLoading()
            if (that.data.list.length==0) {
              that.setData({
                noData:true
              })
            }else{

              that.setData({
                noData:false
              })
            }
          })
        }else{
          that.setData({
            noData:true
          })
          wx.hideLoading()
          app.errorMsg('没有搜到该物品')
        }
      }else{
        wx.hideLoading()
        app.errorMsg('没有搜到该物品')
      }
    })
  },
  //添加入库
  addToStock(e){
    console.log(e)
    var that = this
    let data = {
      account:this.data.account,
      agentId:this.data.agentId,
      stockSkuAllId:e.currentTarget.dataset.id
    }
    console.log(data)
    app.request('wx_app/agent/stock/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        
        that.getGoodsList(1,that.data.searchKey)
        that.setData({
          page:2
        })
      }
    })
  },
  chooseAll(){
    this.setData({
      topCurrentId:'all',
      parent_id:0,
      leftCurrentId:'all',
      goodsType:0
    })
    this.getGoodsTypeList(this.data.agentId,0)
    // this.getGoodsList(1,this.data.searchKey)
  },
  chooseAllLeft(){
    this.setData({
      leftCurrentId:'all',
      page:2
    })
    if (this.data.topCurrentId=='all') {
      this.setData({
        goodsType:0
      })
    }
    // this.getGoodsTypeList(this.data.agentId,0)
    this.getGoodsList(1,this.data.searchKey)
  },
  chooseFirstType(e){
    let id = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    let parent = e.currentTarget.dataset.parent
    this.setData({
      topCurrentId:id,
      parent_id:type,
      leftCurrentId:'all',
      page:2,
      goodsType:type,
      parentType:0,
    })
    this.getGoodsTypeList(this.data.agentId,type)
  },
  bindLeftBtn(e){
    let type = e.currentTarget.dataset.type
    let parent = e.currentTarget.dataset.parent
    console.log(parent)
    if (parent==0) {
      if (this.data.type==0) {
        //全部 全部
        this.setData({
          goodsType:0,
          parentType:0,
        })
        console.log('全部 全部')
      }else{
        //其他 全部
        this.setData({
          goodsType:this.data.parent_id,
          parentType:0,
        })
        console.log('其他 全部')
      }
    }else{
      //其他 其他
      this.setData({
        goodsType:type,
        parentType:parent,
      })
      console.log('其他 其他')
    }
    this.setData({
      leftCurrentId:e.currentTarget.dataset.id,
      page:2
    })
    this.getGoodsList(1,this.data.searchKey)
  },
  //获取一级分类
  getFirstTypeList(agentId){
    const that = this
    let topBtn = this.data.topBtn
    let data = {
      agentId,
      requestType:'agent'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      // topBtn.push({name:"整车类",id:2,parent:0})
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0&&item.id!=98) {
          console.log(item)
          topBtn.push({name:item.name,id:item.id,parent:item.parent_id})
        }
      })
      that.setData({
        topBtn
      })
    })
  },
  //获取商品种类
  getGoodsTypeList(agentId,type){
    const that = this
    let leftBtn = [],
        backBox = [],
        parentType='',
        data = {
          agentId,
          requestType:'agent'
        }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (type==item.parent_id&&type!=0) {
          leftBtn.push({name:item.name,id:item.id,parent:item.parent_id})
        }else if (type==0) {
          if (item.parent_id!=0) {
            leftBtn.push({name:item.name,id:item.id,parent:item.parent_id})
          }
        }
        // if (type==item.id) { 
        //   backBox.push({name:item.name,id:item.id,parent:item.parent_id})
        // }
      })
      that.setData({
        leftBtn,
        // backBox
      })
      // if (that.data.leftBtn==''&&that.data.topCurrentId=='all') {
      //   res.data.product_types.forEach(item=>{
      //     leftBtn.push({name:item.name,id:item.id})
      //   })
      //   that.setData({
      //     leftBtn
      //   })
      // }
      // if (that.data.leftBtn=='') {
      //   that.setData({
      //     leftBtn:that.data.backBox
      //   })
      // }
      // goodsType = that.data.leftBtn[0].id
      // parentType = that.data.leftBtn[0].parent
      // that.setData({
      //   goodsType,
      //   parentType
      // })
      that.getGoodsList(1,this.data.searchKey)
    })
  },
  bindMore(){
    wx.showLoading({
      title:'正在加载'
    })
    let page = this.data.page
    var that = this
    var data = {
      pageNum:page,
      agentId:this.data.agentId,
      type:this.data.goodsType,
      searchKey:this.data.searchKey,
    }
    let list = this.data.list
    console.log(data)
    app.request('wx_app/stock_all/sku/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.product_name,
            price:item.price,
            url:item.main_img_url,
            id:item.id,
            type:item.product_type,
            isAdded:item.isAdded,
            companyName:item.productTag,
            color:item.tagColor
          })
          that.setData({
            list
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      page:page
    })
  },
})